package com.putong35.common.httprequest;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.putong35.common.utils.SLog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Donald Peng
 * @version 1.0 2018-03-28
 *
 * 服务端返回的数据情况
 * 结构限定为：
 * {
 *     code:100,
 *     message:"",
 *     data:{}
 * }
 */
public class Response implements Serializable{
    public static final String TAG = Response.class.getSimpleName();

    public static final String KEY_MESSAGE = "message";
    public static final String KEY_CODE = "code";
    public static final String KEY_DATA = "data";
    public static final String KEY_LIST = "list";
    public static final int SUCCESS_CODE = 1;


    public final static int ERR_CODE_INVALID_NET = -1;//  网路异常，可能断开了or其他情况
    public final static String ERR_STR_INVALID_NET = "网络不可用，请检查网络";

    public final static int ERR_CODE_SERVER_ERROR = -2;//  服务器错误
    public final static String ERR_STR_SERVER_ERROR = "服务器异常，请联系开发人员";

    private JSONObject mResponseJobj;

    public Response(String responseJsonString) {
        setDataString(responseJsonString);
    }

    public void setDataString(String jsonStr) {
        try {
            mResponseJobj = new JSONObject(jsonStr);
        } catch (JSONException e) {
            mResponseJobj = new JSONObject();
            try {
                SLog.w(TAG, "服务器返回数据无法正常转json，对其进行json封装");
                mResponseJobj.put(KEY_MESSAGE, "success");
                mResponseJobj.put(KEY_CODE, SUCCESS_CODE);
                mResponseJobj.put(KEY_DATA, jsonStr);
            } catch (JSONException e1) {
                throw new RuntimeException("服务器返回数据有异常：Response.class");
            }
        }
    }


    /**
     * mResponseJobj 必须不为空， 这个在 isMyRules 会做判断
     *
     * @return
     */
    public boolean isSuccessful() {
        return getCode() == SUCCESS_CODE;
    }


    /**
     * mResponseJobj 必须不为空， 这个在 isMyRules 会做判断
     *
     * @return
     */
    public String getMessage() {
        return mResponseJobj.optString(KEY_MESSAGE);
    }

    /**
     * mResponseJobj 必须不为空， 这个在 isMyRules 会做判断
     *
     * @return
     */
    public int getCode() {
        return mResponseJobj.optInt(KEY_CODE);
    }

    /**
     * @return 返回String类型的Data
     */
    public String getDataString() {
        String datString = mResponseJobj.optString(KEY_DATA);
        return TextUtils.isEmpty(datString) ? "" : datString;
    }

    /**
     *
     * @return 返回JSONObject类型的Data
     */
    public JSONObject getDataJson() {
        JSONObject dataJson = mResponseJobj.optJSONObject(KEY_DATA);
        if (dataJson != null) {
            return dataJson;
        } else {
            return null;
        }
    }

    /**
     *
     * @return 返回模板类型BeanT的Data
     */
    public <BeanT extends Object> BeanT getDataBean(Class clazz) {
        BeanT bean;
        try {
            Gson gson = new Gson();
            bean = (BeanT)gson.fromJson(getDataString(), clazz);

        } catch (Exception e) {
            e.printStackTrace();
            SLog.e(HttpRequest.TAG, "Response getDataListBean 解析错误");
            bean = null;
        }
        return bean;
    }

    protected List beanList;
    public <BeanT extends Object> List<BeanT> getDataListBean(Class clazz) {
        JSONArray array = mResponseJobj.optJSONObject(KEY_DATA).optJSONArray(KEY_LIST);
        List<BeanT> beanList = new ArrayList<>();
        if (array != null && array.length() > 0) {
            Gson gson = new Gson();
            for (int i = 0; i < array.length(); i++) {
                BeanT bean = (BeanT)gson.fromJson(array.opt(i).toString(), clazz);
                beanList.add(bean);
            }
        }
        this.beanList = beanList;
        return beanList;
    }

    public boolean hasNextPage() {
        // 为null说明还没有第一次请求
        if (this.beanList == null || beanList.size() > 0) {
            return true;
        }
        return false;
    }

}
